<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Wishlist extends MY_Controller {

     function __construct() {
     	
        parent::__construct();
        $this->load->helper('url');
        $this->load->library('parser');
        //$this->load->library('cart');
        //$this->template->set_template('home');
        //$this->template->add_region('new_tours');
        $this->load->model('wishlist_model', 'wishlist', true);
        $this->load->model('product_model', 'product', true);
        //$this->load->model('popup_model', 'popup', true);
        //$this->load->model('ncategory_model', 'ncategory', true);
        //$this->buildFrontEndTopMenu();
        //$this->buildSlider();
        
        $this->load->helper('form');
        
        
        //$this->buildSearchByConditionBox();
       // $this->buildSupportBox();
        
        $this->buildNavigation();
		$this->buildAccountBox();

		//$this->buildSearchByConditionBox();
		$this->buildSupportBox();
		
		//$this->buildAdvBox();
		//$this->buildSlider();
		//$this->buildCartBox();
		//$this->buildCompareBox();

     }
    
	function index() {
	
        $data['base_url'] = base_url();
        
        $this->view();
        
        //$this->template->parse_view('center_content1', 'default/front_end/home/new_product_view', $data);
        //$this->buildLeftContent();
        //$this->template->render();  
        
    }
	
    function view1(){
    	$member_id = $this->session->userdata('member_id');
        
    	$data['lb_name'] = lang('name');
    	$data['lb_picture'] = lang('picture');
    	$data['lb_quantity'] = lang('quantity');
    	$data['lb_price'] = lang('price');
    	$data['lb_total'] = lang('total');
    	$data['lb_total_amount'] = lang('total_amount');
    	$data['shopping_cart'] = lang('shopping_cart');
    	$data['home'] = lang('home');
        $data['cart'] = $this->cart->getCartMember( $member_id);
        
       // if (empty($data['products']))
       // 	echo "Shopping cart is empty.";
        
        //print_r($data['products']);
        
        $lang = getLangCode();
        
        $totalAmount = 0;
        
        	foreach ($data['cart'] as &$item){
        		$item['name'] = $item[ $lang . '_name'];
        		$item['img'] = img(array('src' => getDirByDate($item['date_add'], PRODUCT_IMAGE_PATH) . '/' . $item['image'],  'height' => '60', 'width' => '60' ));
        		$item['delete'] = anchor('cart/delete/' . $item['product_id'], 'Delete', 'title="Delete '. $item['name'] .'"');
        		$item['total'] = $item['price'] * $item['quantity'] ;
        		$item['quantity'] = form_input(array(
             					 'name'        => 'quantity[]',
              					//'id'          => 'price',
              					'value'       => $item['quantity'],
              					'maxlength'   => '100',
              					'size'        => '50',
              					'style'       => 'width:50px',
           						 ));
           		$item['product_input'] = form_hidden( 'product_id[]' , $item['product_id']);
           		$totalAmount += $item['total'];
        	}
        $data['update_shopping_cart'] = anchor('#', lang('update_shopping_cart'), ' class="buttonlink" id="update_cart"');
        $data['process_to_checkout'] = anchor('checkout', lang('process_to_checkout'), ' class="buttonlink"');
        $data['continue_shopping'] = anchor('product', lang('continue_shopping'), ' class="buttonlink" title="Continue Shopping"');
        
        $data['action'] = site_url('cart/update');
        $data['total_amount'] = $totalAmount;
        
        $this->template->parse_view('top_content1', 'default/front_end/cart/cart_view',$data);
        $this->template->render();  
    }
    
    
function view($offset = 0){
		
		$user = $this->session->userdata('customer');
		//print_r($user);
		
		$this->load->library('pagination');
		
		$base_url = site_url ( 'member/orders/view');
        $total_rows = $this->wishlist->countWishlistMember($user['id_user']); 
        $per_page = 20; //NUMBER_RECORD_PER_PRODUCT_PAGE;
        $uri_segment = 4;
        
        pagination_front($base_url, $total_rows, $per_page, $uri_segment);
        
		$orders = $this->wishlist->getWishlistMember($user['id_user']);
		
		foreach ($orders as &$item){
			$item['detail_url'] = site_url('member/orders/detail/' . $item['product_id']);
			$item['img'] = img(array('src' => getDirByDate($item['date_add'], PRODUCT_IMAGE_PATH) . '/' . $item['image'],  'height' => '60', 'width' => '60' ));
			$item['add_cart_link'] = anchor('cart/add/' . $item['product_id'], 'Add to cart', 'title="Add to cart ' .'"');
			$item['add_compare_link'] = anchor('compare/add/' . $item['product_id'], 'Add to compare', 'title="Add to compare"');
			$item['delete_link'] = anchor('member/wishlist/delete/' . $item['product_id'], 'Delete', 'title="Delete"');
			$item['detail_link'] = anchor('product/detail/' . $item['code'], 'Detail', 'title="Add to cart"');
		}
		
//		print_r($orders);
		$data['items'] = $orders;
		
		$this->template->parse_view('right_content2column', $this->skins.'member/wishlist/wishlist_view', $data);
		$this->template->render();
	}
	
	function add($product_id) {
		
    	//$member_id = $this->session->userdata('member_id');
    	
		$user = $this->session->userdata('customer');
    	$member_id = $user['id_user'];
    	
    	if (empty($member_id)){
    		redirect('login');
    		return;
    	}

    	$product = $this->product->getById($product_id);
    	
    	if (!empty($product)){
    		$item = $this->wishlist->getWishlistMember($member_id, $product_id);
    		if (empty($item))
    			$result = $this->wishlist->insert(array('member_id' => $member_id, 'product_id' => $product_id, 'quantity' => 1));
    	}
    	
    	$this->view();
    }
    
	function delete() {
		
    	$user = $this->session->userdata('customer');
    	$member_id = $user['id_user'];
    	
		if (empty($member_id)){
    		redirect('login');
    		return;
    	}
    	
    	$product_id = $this->uri->segment(4);
    	
    	$this->wishlist->delete($member_id, $product_id);
    	
    	$this->view();
    }

  
}

?>